<?php
/**
 * @author Created by dungtn
 * @version 1.0
 * @package Fdoc
 * @subpackage FdocView
 *
 */

_fwiload::package('Smarty2.6.26', 'libs/Smarty.class');
class SmartyView {

    protected $_objSmarty = null;

    /**
     * Constructor method
     *
     * @param FdocController $objCtrl
     */
    public function __construct() {
        $conf = _fwiload::loadConfig(_fwia::AppName(), 'common');
        
        $this->_objSmarty = new Smarty();

        $this->_objSmarty->cache_lifetime =3;
        $this->_objSmarty->cache_dir    = _fwia::AppPath().'server'.DSR.'temp'.DSR. 'template_cache';
        $this->_objSmarty->template_dir =  _fwia::AppPath().'server'.DSR.'template';
        $this->_objSmarty->compile_check = $conf['smarty_compile_check'];
        $this->_objSmarty->compile_dir  = _fwia::AppPath().'server'.DSR.'temp'.DSR. 'template_compile';
        $this->_objSmarty->config_dir   = _fwia::AppPath().'server'.DSR.'temp'.DSR. 'template_config';
        $this->_objSmarty->debugging = $conf['smarty_debug'];
        
        $this->_objSmarty->register_function("jsload", "_fwiSmartyLoadJs");
        $this->_objSmarty->register_function("cssload", "_fwiSmartyLoadCss");
    }

    /**
     * Set template dir
     *
     * @param string $dir
     */
    function setTemplateDir($dir) {
        if (!$dir) {
            throw new Exception("template dir is not valid!");
        }
        $this->_objSmarty->template_dir = $dir;
    }

    /**
     * get template dir
     *
     * @return string
     */
    public function getTemplateDir() {
        return $this->_objSmarty->template_dir;
    }


    /**
     * call method assign() of object Smarty
     *
     * @param string $key
     * @param string $value
     */
    public function assign($key, $value) {
        $this->_objSmarty->assign($key, $value);
    }

    /**
     * display part of page
     *
     * @param string $partName
     * @param array $vars
     * @param boolean $clearFlag
     */
    public function viewPart($partName, & $vars = null, $clearFlag = 0) {

        if ($clearFlag)
            $this->_objSmarty->clear_all_assign();

        if (!isset($vars))
            $this->_objSmarty->display($partName . ".html");

        elseif (is_array($vars)) {
            foreach ($vars as $key => $value) {
                $this->_objSmarty->assign($key, $value);
            }

            $this->_objSmarty->display($partName . ".html");
        }

        else {
            throw new fwiException("[viewpart] \$vars must be an array");
        }
    }

    /**
     * return part of page as HTML string
     *
     * @param string $partName
     * @param array $vars
     * @param boolean $clearFlag
     */
    public function getPart($partName, & $vars = null, $clearFlag = 0) {
        if ($clearFlag) {
            $this->_objSmarty->clear_all_assign();
        }

        try {
            if (is_array($vars)) {
                foreach ($vars as $key => $value) {
                    $this->_objSmarty->assign($key, $value);
                }
            }

            return $this->_objSmarty->fetch($partName . ".html");
        } catch (fwiException $e) {
            throw $e;
        }
    }
    public function viewJsonData($paramName){        
        echo json_encode($this->_objSmarty->_tpl_vars[$paramName]);
    }
    public function warring($message, $exit = true){
        $result['status'] = 1;
        $result['message'] = $message;
        if ($exit){
            exit();
        }
    }
}


function _fwiSmartyLoadJs($params){
    return _fwia::BaseURL().'js/'.$params['url'];
}

function _fwiSmartyLoadCss($params){
    return _fwia::BaseURL().'css/'.$params['url'];
}
